package com.cljh.controller;

import com.cljh.bean.Article;
import com.cljh.bean.Comment;
import com.cljh.config.AuthAccess;
import com.cljh.controller.page.Code;
import com.cljh.controller.page.Result;
import com.cljh.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    CommentService commentService;
    @AuthAccess
    @GetMapping("/tree/{id}")
    public Result getById(@PathVariable Integer id) {
        List<Comment> comments = commentService.selectByArticleId(id);//查询所有的评论数据
        //筛选根评论（不包括回复的评论）
        List<Comment> collect = comments.stream()
                        .filter(comment -> comment.getOriginId() == null)
                        .collect(Collectors.toList());
        //遍历根评论，设置评论数据的子节点
        for (Comment origin :
                collect) {
            List<Comment> list = comments.stream()
                            .filter(comment -> origin.getId().equals(comment.getOriginId()))
                            .collect(Collectors.toList());
            comments.forEach(comment -> {
                //找到父级评论的用户id和用户昵称，并设置给当前回复的对象
                comments.stream().filter(comment1 -> comment1.getId().equals(comment.getPid())).findFirst().ifPresent((v->{
                    if (v.getName()!=null){
                        comment.setPNickname(v.getName());
                    }else {
                        comment.setPNickname(v.getNickname());
                    }
                    if (v.getAdminId()!=null){
                        comment.setPUserId(v.getAdminId());
                    }else {
                        comment.setPUserId(v.getUserId());
                    }
                }));
            });
            origin.setChildren(list);
        }


        Integer code = collect != null ? Code.GET_OK : Code.GET_ERR;
        String msg = collect != null ? "查询到信息" : "未查询到信息";
        return new Result(code, collect, msg);
    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean delete = commentService.deleteById(id);

        Integer code = delete ? Code.DELETE_OK : Code.DELETE_ERR;
        String msg = delete ? "删除一个成功" : "删除一个失败";
        return new Result(code, delete, msg);
    }

    @PostMapping
    public Result save(@RequestBody Comment comment) {
        Integer pid = comment.getPid();
        if (pid!=null){
            Comment pComment = commentService.selectById(pid);
            if (pComment.getOriginId()!=null){
                comment.setOriginId(pComment.getOriginId());
            }else {
                comment.setOriginId(comment.getPid());
            }

        }

        boolean save = commentService.save(comment);
        Integer code = save ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = save ? "添加成功" : "添加失败";
        return new Result(code, save, msg);
    }

    @PutMapping
    public Result update(@RequestBody Comment comment) {
        boolean update = commentService.update(comment);

        Integer code = update ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String msg = update ? "修改成功" : "修改失败";
        return new Result(code, update, msg);
    }

}
